package com.xinsoft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesProcessReportPageParam;
import com.xinsoft.entity.po.MesDocument;
import com.xinsoft.entity.po.MesProcessReport;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.entity.vo.MesDispatchVo;
import com.xinsoft.entity.vo.MesProcessReportVo;
import com.xinsoft.service.MesDocumentService;
import com.xinsoft.service.MesProcessReportService;
import com.xinsoft.mapper.MesProcessReportMapper;
import com.xinsoft.service.SysFileHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 13370
* @description 针对表【mes_process_report(工序报工)】的数据库操作Service实现
* @createDate 2023-07-31 16:05:10
*/
@Service
public class MesProcessReportServiceImpl extends ServiceImpl<MesProcessReportMapper, MesProcessReport>
    implements MesProcessReportService{


    @Autowired
    private MesDocumentService mesDocumentService;
    @Resource
    SysFileHisService fileHisService;

    @Override
    public Response<?> findPageListByParam(MesProcessReportPageParam mesDispatchPageParam) {
        Page<MesProcessReportVo> page = new Page<>(mesDispatchPageParam.getCurrentPage(), mesDispatchPageParam.getPageSize());
        IPage<MesProcessReportVo> pageParamList = this.baseMapper.findPageListByParam(page, mesDispatchPageParam);
        List<MesProcessReportVo> records = page.getRecords();
        if (ObjectUtil.isNotEmpty(records)){
            List<Integer> ids = records.stream().map(MesProcessReportVo::getId).collect(Collectors.toList());
            List<MesDocument> documentDOS = mesDocumentService.list(new LambdaQueryWrapper<MesDocument>()
                    .eq(MesDocument::getMesType, Const.FileType.MES_REPORT)
                    .in(MesDocument::getMesId, ids));
            Map<Integer, MesDocument> documentDOSMap = documentDOS.stream().collect(Collectors.toMap(item -> item.getMesId(), item -> item, (v1, v2) -> v1));
            records.forEach(item -> item.setHasDocument(documentDOSMap.containsKey(item.getId())));
        }
        return Response.succeed(pageParamList);
    }

    @Override
    public Response<?> getDocument(Integer id) {
        List<MesDocument> documentDOS = mesDocumentService.list(new LambdaQueryWrapper<MesDocument>()
                .eq(MesDocument::getMesType, Const.FileType.MES_REPORT)
                .eq(MesDocument::getMesId, id));
        if (ObjectUtil.isEmpty(documentDOS)){
            return Response.fail("该报工记录未上传图片");
        }
        List<Integer> documentIds = documentDOS.stream().map(MesDocument::getDocumentId).collect(Collectors.toList());
        List<SysFileHis> sysFileHis = fileHisService.listByIds(documentIds);
        return Response.succeed(sysFileHis);
    }
}




